package com.hugoo.core.db.rp;

import com.hugoo.core.util.TL;
import com.hugoo.extra.xlsx.Exl;
import com.hugoo.rpt.ExpPageInfo;
import java.sql.ResultSet;

/**
 *
 * @author Lovelock.Chan
 */
public class XlsxResultPack extends ResultPack {
    
    public XlsxResultPack(ExpPageInfo pi, String[][] col) {
        super(pi, col);
    }
    
    @Override
    public void packResult(ResultSet rs) {
        
        try {
            /////////验证列是否有效///////////////
            pi.validateColumn(rs.getMetaData());
            ///////////////////////////////
            int i = 0, c = col[0].length;
            boolean[] b = new boolean[col[0].length];
            String fnum = pi.getFNUMColumns();
            for (int z = 0; z < c; z++) {
                b[z] = fnum.contains("," + col[0][z] + ",");
            }
            //////////////创建Xlsx大文本输出类/////////////
            Exl xl = new Exl(pi.getTempFile());
            xl.setTitle(pi.getTitle());
            xl.addRowTitle(col[1]);
            ////////////////////////////////
            while (rs.next()) {
                xl.addRow(i);
                for (int z = 0; z < c; z++) {
                    if (b[z]) {//以数值方式插入数据值
                        xl.createCell(z, prs(getValue(rs, col[0][z])), Exl.STYLE_NUM);
                    } else {
                        xl.createCell(z, getValue(rs, col[0][z]), Exl.STYLE_TXT);
                    }
                }
                xl.packRow();
                i++;
            }
            if (i > 0) {
                xl.pack();
            }
            /////////////////////////
        } catch (Exception ex) {
            log.warn("导出Excel文件出错,请核查!", ex);
        }
    }
    
    private double prs(String vl) {
        if (TL.isEmpty(vl)) {
            return 0;
        }
        return TL.parseDouble(vl.replaceAll("[^0-9.-]", ""));
    }
}
